package cn.edu.cdu.farmdeal.controller;

import cn.edu.cdu.farmdeal.domain.Miaosha;
import cn.edu.cdu.farmdeal.service.IndexService;
import cn.edu.cdu.farmdeal.service.ProductService;
import cn.edu.cdu.farmdeal.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Author: tupeng
 * @Date: 2020/3/13
 **/
@Controller
public class IndexController {
    @Autowired
    private ProductService productService;
    @Autowired
    private IndexService indexService;

    @PostMapping("/getOrderNum")
    @ResponseBody
    public R getOrderNum(String userName) {
        return R.ok().put("data", indexService.getOrderNum(userName));
    }

    @GetMapping("/getRecommend")
    @ResponseBody
    public R getRecommend(String userid) {
        return R.ok().put("data", indexService.getRecommend(userid));
    }

    @RequestMapping("/index.html")
    public ModelAndView add(ModelAndView modelAndView, HttpServletRequest request) {
        HttpSession session1 = request.getSession();
//        session1.setAttribute("userid", "admin");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/cart.html")
    public ModelAndView cart(ModelAndView modelAndView, HttpServletRequest request) {
        HttpSession session1 = request.getSession();
//        session1.setAttribute("userid", "admin");
        modelAndView.setViewName("cart");
        return modelAndView;
    }

    @GetMapping("/getSuggest")
    @ResponseBody
    public R getSuggest(String InputValue) throws Exception {
        return R.ok("请求成功").put("data", indexService.getSuggest(InputValue));
    }

    @GetMapping("/getMiaosha")
    @ResponseBody
    public R getMiaosha() {
        HashMap map = new HashMap(10);
        map.put("goods_list", indexService.getMiaosha());
        Miaosha miaosha = indexService.getMsTime();
        map.put("ms_time", miaosha.getMsTime());
        return R.ok("请求成功").put("data", map);
    }

    @PostMapping("/getShopcartNum")
    @ResponseBody
    public R getShopcartNum(String userName) {
        return R.ok("请求成功").put("data", indexService.getCartNum(userName));
    }

    @GetMapping("/getHotSale")
    @ResponseBody
    public R getHotSale() {
        return R.ok("请求成功").put("data", indexService.getHotSale());
    }


}
